//
//  XOShopCartViewModel.m
//  didigo
//
//  Created by JS_Coder on 2019/3/10.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XOShopCartViewModel.h"
#import "XOShoppingCartListModel.h"
#import "XOShoppingCartListRequest.h"
@interface XOShopCartViewModel()

@end

@implementation XOShopCartViewModel
static XOShopCartViewModel * _instance = nil;
typedef void (^finished)(XOShoppingCartListModel *response);

+(instancetype)shared
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init] ;
    }) ;
    return _instance ;
}

+(id) allocWithZone:(struct _NSZone *)zone
{
    return [XOShopCartViewModel shared] ;
}

-(id) copyWithZone:(struct _NSZone *)zone
{
    return [XOShopCartViewModel shared] ;
}


- (void)updateCartList: (finished)completed {
    if(![EJSUserTokenModel userTokenModel]){
        return;
    }

    FlyWeakSelf
    XOShoppingCartListRequest *req = [[XOShoppingCartListRequest alloc] init];
    req.hideLoadingView = YES;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.shoppingCartListModel = response.data;
            NSArray * items = [UIApplication  sharedApplication].keyWindow.rootViewController.childViewControllers;
            FlyNavigationController *navi = items[3];
            [navi.tabBarItem setBadgeColor:AppSecondMainColor];
        if(weakSelf.shoppingCartListModel.list.count > 0){
            [navi.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%lu", (unsigned long)weakSelf.shoppingCartListModel.total.intValue]];
        }else{
            [navi.tabBarItem setBadgeValue:NULL];
            [navi.tabBarItem setBadgeColor:NULL];
        }
        completed(weakSelf.shoppingCartListModel);
    } failHandler:^(BaseResponse *response) {
        completed(weakSelf.shoppingCartListModel);
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

- (void)updateGoodsAddedCount {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateGoodsAddedCount" object:NULL];
}

@end
